package cn.lm.activit.module;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;

import java.io.File;
import java.io.InputStream;

/**
 * @author lm_jcf
 * @date 2018/7/20 18:22
 */
public class ModelHandle {

    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

    @Test
    public void createProcdefBpmn() throws Exception {
        String processId = "50001";

        ProcessInstance processInstance = processEngine.getRuntimeService().createProcessInstanceQuery()
                .processInstanceId(processId)
                .singleResult();
        String processKey = null;
        String procDefinitionId = null;
        String deploymentId = null;
        String processDefinitionId = null;

        if (processInstance == null) {
            HistoricProcessInstance historicProcessInstance = processEngine.getHistoryService().createHistoricProcessInstanceQuery()
                    .processInstanceId(processId)
                    .singleResult();
            processKey = historicProcessInstance.getProcessDefinitionKey();
            procDefinitionId = historicProcessInstance.getProcessDefinitionId();
            deploymentId = historicProcessInstance.getDeploymentId();
            processDefinitionId =processInstance.getProcessDefinitionId();
        }else {
            processKey =processInstance.getProcessDefinitionKey();
            procDefinitionId = processInstance.getProcessDefinitionId();
            deploymentId = processInstance.getDeploymentId();
            processDefinitionId =processInstance.getProcessDefinitionId();
        }
        ProcessDefinition processDefinition = processEngine.getRepositoryService().getProcessDefinition(processDefinitionId);


        if (StringUtils.isBlank(processKey) || StringUtils.isBlank(procDefinitionId) || StringUtils.isBlank(deploymentId)) {
            throw new Exception("数据不存在");
        }

        //6、保存流程图png文件
        InputStream in = processEngine.getRepositoryService()
                .getProcessDiagram(procDefinitionId);

        Deployment deployment = processEngine.getRepositoryService().createDeploymentQuery()
                .deploymentId(deploymentId)
                .singleResult();

        FileUtils.copyInputStreamToFile(in,new File("/deployments/"+processKey+".png"));
        //7、保存流程bpmn文件
        InputStream processBpmn = processEngine.getRepositoryService()
//                .getResourceAsStream(deployment.getId(),"bpmn/"+processKey+".bpmn");
                .getResourceAsStream(deployment.getId(),processDefinition.getDiagramResourceName());
        FileUtils.copyInputStreamToFile(processBpmn, new File("deployments/"+processId+".bpmn"));
        System.out.println("输出完成");

    }
}
